﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Process
{
    public partial class InsertProcess : BasePage
    {
        private Lc_ProcessBLL lpb = new Lc_ProcessBLL();
        private Sys_CompanyBLL scb = new Sys_CompanyBLL();
        private static DataTable dtCompany = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCompany();
            }

            lblCzRen.Text = lognUser.USER_NAME;
            lblCzTime.Text = DateTime.Now.ToString();
        }

        private void BindCompany()
        {
            DataTable tableOrganization = scb.GetInfoByPId((int)lognUser.USER_COMPANY);

            if (tableOrganization != null && tableOrganization.Rows.Count > 0)
            {
                setList(tableOrganization, ddlCompany);
            }
            else
            {
                ddlCompany.Items.Add(new ListItem("无", "0"));
            }
        }

        private void setList(DataTable dt, System.Web.UI.WebControls.DropDownList lb)
        {
            int idx = -1;
            dtCompany = dt;
            for (int n = 0; n < dtCompany.Rows.Count; n++)
            {
                if (dtCompany.Rows[n]["CMY_PARENTID"].ToString() == "0")
                {
                    idx = idx + 1;
                    addItem(lb, dtCompany.Rows[n]["CMY_ID"].ToString(), dtCompany.Rows[n]["CMY_NAME"].ToString(), idx);
                    idx = idx - 1;
                }
            }
        }

        private void addItem(System.Web.UI.WebControls.DropDownList lb, String sId, String sName, int idx)
        {
            String sn = sName;
            for (int n = 0; n < idx; n++)
            {
                sn = (char)58853 + "" + sn;
            }
            System.Web.UI.WebControls.ListItem it = new ListItem(sn, sId);

            lb.Items.Add(it);

            for (int n = 0; n < dtCompany.Rows.Count; n++)
            {
                if (dtCompany.Rows[n]["CMY_PARENTID"].ToString() == sId)
                {
                    idx = idx + 1;
                    addItem(lb, dtCompany.Rows[n]["CMY_ID"].ToString(), dtCompany.Rows[n]["CMY_NAME"].ToString(), idx);
                    idx = idx - 1;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Lc_Process model = new Lc_Process();
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                MessageBox.Show(Page, "流程名称不能为空");
                return;
            }

            //判断该流程是否存在
            DataTable dt = lpb.GetAllInfo("ProcessName", txtName.Text);
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show(Page, "该流程已经存在");
                return;
            }
            model.C_PROCESS_NAME = txtName.Text.Trim();

            model.N_PROCESS_STATE = 1;

            model.C_PROCESS_REQUESTION_COM = txtSQGL.Text.Trim();

            model.N_PROCESS_OPERATOR_ID = decimal.Parse(lognUser.USER_ID.ToString());

            model.D_PROCESS_OPERATOIN_TIME = DateTime.Now;

            model.COMPANYID = decimal.Parse(ddlCompany.SelectedValue);

            model.TIPTIME = 0;

            int count = lpb.INSERT(model, lognUser.USER_NAME);
            if (count > 0)
            {
                MessageBox.Show(Page, "保存成功");
                ClearControl();
                return;
            }
            else
            {
                MessageBox.Show(Page, "保存失败");
                return;
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("SelectProcess.aspx");
        }

        private void ClearControl()
        {
            txtName.Text = "";
            txtSQGL.Text = "";
        }
    }
}